package com.xiaoyao.myframeapp.utils;

import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;

import java.security.MessageDigest;

/**
 * @Author xiaoyao
 * @Email xiao80206040@qq.com
 * @Explain TODO 小键盘工具
 * @Created by Administrator on 2017/8/8 0008 10:26
 */
public class KeypadTools {
    /**
     * 隐藏键盘
     */
    public static void hideKeyBord(Activity activity) {
        try {
            ((InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(activity.getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示键盘
     */
    public static void showKeyBord(Context context) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
    /**
     * 微信加密
     */
    public static String encoderByMd5(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(str.getBytes());

            byte[] b = digest.digest();

            final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            StringBuilder sb = new StringBuilder(b.length * 2);
            for (int i = 0; i < b.length; i++) {
                String result = Integer.toHexString(b[i] & 0xFF);//字节装换成十六进制
                int k = (b[i] & 0xf0);
                sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
                sb.append(HEX_DIGITS[b[i] & 0x0f]);
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
